package com.asset.controller;


import com.asset.entity.Purchase;
import com.asset.service.PurchaseService;
import com.asset.utils.RedisUtils;
import com.asset.vo.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 购置表控制层
 *
 */
@RestController
@RequestMapping("/purchase")
public class PurchaseController {
    @Autowired
    PurchaseService purchaseService;

    //返回购置信息列表
    @RequestMapping("list")
    public AjaxJson list() {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            List<Purchase> lists = purchaseService.purchaseList();
            ajaxJson.putData("purchaseList",lists);
            ajaxJson.setSuccess("true");
            ajaxJson.setMsg("购置信息查询成功");

        }catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setMsg("购置信息查询失败");
        }

        return ajaxJson;
    }

    //根据购置编号返回信息列表
    @RequestMapping("getById")
    public AjaxJson getById(String id) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            Purchase purchase = purchaseService.getById(id);
            ajaxJson.setSuccess("true");
            ajaxJson.setMsg("购置信息查询成功");
            ajaxJson.putData("purchase",purchase);

        }catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setMsg("购置信息查询失败");
        }

        return ajaxJson;
    }

    //添加购置表
    @RequestMapping("add")
    public AjaxJson add(@RequestBody Purchase purchase) {

        AjaxJson ajaxJson = new AjaxJson();
        try {
            //添加
            purchaseService.addPurchase(purchase);
            ajaxJson.setSuccess("true");
            ajaxJson.putData("purchase",purchase);

        }catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setMsg("购置信息查询失败");
        }
        return ajaxJson;
    }

    //自动生成购置编号
    @RequestMapping("addNumber")
    public AjaxJson addNumber(@RequestBody Purchase purchase) {
        AjaxJson ajaxJson = new AjaxJson();
        RedisUtils redisUtils = new RedisUtils();
        try {
            String number = redisUtils.createNumber();
            ajaxJson.putData("number",number);
        }catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setMsg("购置编号生成失败");
        }
        return ajaxJson;
    }


}
